package br.com.consultsystems.persistencia.impl.financeiro;

import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import br.com.consultsystems.dominio.financeiro.Balancete;
import br.com.consultsystems.dominio.tipos.StatusFinanceiro;
import br.com.consultsystems.persistencia.spec.AbstractAdministradoraRepository;
import br.com.consultsystems.persistencia.spec.financeiro.BalancetePersistencia;

@Stateless(name = "balancetePersistencia")
public class BalancetePersistenciaImpl extends
		AbstractAdministradoraRepository<Balancete> implements
		BalancetePersistencia {

	protected BalancetePersistenciaImpl() {
		super(Balancete.class);
	}

	@Override
	public List<Balancete> retrieveAll() {
		return retrieveByCriteria(null);
	}

	@Override
	public List<Balancete> retrieveByNomeCondominio(String nomeCondominio) throws Exception {
		
		final EntityManager em = getEntityManager();
		final CriteriaBuilder criteriaBuilder = createCriteriaBuilder();
		final CriteriaQuery<Balancete> criteriaQuery = createCriteriaQuery(criteriaBuilder);
		final Root<Balancete> root = createRoot(criteriaQuery);

		/* Repository -> Apenas pesquias as informações no banco de dados
		 * Filtro por codigo */
		final Path<Long> pathCondominioId = root.get("condominio").get("id");
		final Path<Date> pathDataVencimento = root.get("dataVencimento");
		final Path<String> pathCondominioNome = root.get("condominio").get("nome");
		final Predicate predicateCondominioNome = criteriaBuilder.like(pathCondominioNome, "%"+nomeCondominio+"%");
		
		criteriaQuery.where(predicateCondominioNome);
		criteriaQuery.orderBy(criteriaBuilder.asc(pathCondominioId), criteriaBuilder.asc(pathDataVencimento));
		
		final TypedQuery<Balancete> typedQuery = em.createQuery(criteriaQuery);
		
		return typedQuery.getResultList();
	}

	@Override
	public List<Balancete> retrieveByIdCondominio(Long idCondominio) {

		final EntityManager em = getEntityManager();
		final CriteriaBuilder criteriaBuilder = createCriteriaBuilder();
		final CriteriaQuery<Balancete> criteriaQuery = createCriteriaQuery(criteriaBuilder);
		final Root<Balancete> root = createRoot(criteriaQuery);

		final Path<Date> pathDataVencimento = root.get("dataVencimento");
		final Path<Long> pathIdCondominio = root.get("condominio").get("id");
		final Path<StatusFinanceiro> pathStatusFinanceiro = root.get("status");

		final Predicate predicateIdCondominio = criteriaBuilder.equal(pathIdCondominio, idCondominio);
		final Predicate predicateStatusFinanceiro = criteriaBuilder.equal(pathStatusFinanceiro, StatusFinanceiro.ABERTO);
		
		criteriaQuery.where(predicateIdCondominio, predicateStatusFinanceiro);
		criteriaQuery.orderBy(criteriaBuilder.asc(pathDataVencimento));
		
		final TypedQuery<Balancete> typedQuery = em.createQuery(criteriaQuery);
		return typedQuery.getResultList();
	}

}
